package dynamic.toolkit;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.github.yulichang.base.MPJBaseMapper;

import dynamic.base.DMPJBaseMapper;
import dynamic.mapper.DBaseMapper;
import dynamic.metadata.EntityClass;
import dynamic.mybatisplus.core.toolkit.DSpringContentUtils;

import java.util.function.Function;

public class DSqlHelper {
    public static <R, T> R exec(EntityClass entityClass, Function<DBaseMapper, R> function) {
        Assert.notNull(entityClass,"请使用 new MPJLambdaWrapper(主表.class) 或 JoinWrappers.lambda(主表.class) 构造方法");
        Object mapper = DSpringContentUtils.getMapper(entityClass);
        Assert.notNull(mapper, "mapper not init <%s>", entityClass.getSimpleName());
        return function.apply((DBaseMapper) mapper);
    }

    public static <R, T> R execJoin(EntityClass entityClass, Function<DMPJBaseMapper, R> function) {
        Assert.notNull(entityClass,"请使用 new MPJLambdaWrapper(主表.class) 或 JoinWrappers.lambda(主表.class) 构造方法");
        Object mapper = DSpringContentUtils.getMapper(entityClass);
        Assert.notNull(mapper, "mapper not init <%s>", entityClass.getSimpleName());
        Assert.isTrue(mapper instanceof MPJBaseMapper, "mapper <%s> not extends MPJBaseMapper ", entityClass.getSimpleName());
        return function.apply((DMPJBaseMapper) mapper);
    }
}
